package org.example.domain.shop.service.discount.filter;


import org.example.domain.shop.model.entity.DiscountCalculationContextEntity;
import org.example.domain.shop.model.entity.DiscountCalculationLinkParamEntity;
import org.example.types.annotation.DCCValue;
import org.example.types.design.link.AbstractLogicLinkNode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/8 00:24
 */

@Component
public class SampledUserFilter extends AbstractLogicLinkNode<DiscountCalculationLinkParamEntity, DiscountCalculationContextEntity, BigDecimal> {

    @DCCValue("cutRange:100")
    private static String cutRange;

    private static final int HASH_MODULUS = 100;

    @Override
    public BigDecimal execute(DiscountCalculationLinkParamEntity discountCalculationLinkParamEntity, DiscountCalculationContextEntity discountCalculationContextEntity) {
        // 计算用户hashcode
        int hashCode = discountCalculationLinkParamEntity.getUserId().hashCode();
        // 计算用户hashcode在cutRange中的位置
        if(hashCode % HASH_MODULUS < Integer.parseInt(cutRange)){
            discountCalculationContextEntity.setDiscount(discountCalculationContextEntity.getDiscount().add(new BigDecimal(10)));
        }
        return executeNext(discountCalculationLinkParamEntity, discountCalculationContextEntity);
    }
}
